package com.adds.lvds.security.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.security.acegi.utils.MenuUtil;
import com.adds.lvds.security.model.SysResource;
import com.adds.lvds.security.service.SysResourceService;

/**
 * 
 * 项目名称 ：lvds-web-security
 * 
 * @author zhangjian
 * @time 2017-3-23下午12:12:19
 */

@Controller
@RequestMapping(value = "/security/resources/")
public class ResourcesController extends BaseController<SysResource> {
	@Inject
	private SysResourceService resourcesService;

	/**
	 * 跳转到菜单管理列表页
	 * 
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(Model model, HttpServletRequest request) {
		SysResource t = new SysResource();
		t.setParentId(0);
		List<SysResource> list = resourcesService.queryAll(t);
		String htmlBut = (String) request.getAttribute("htmlBut");
		model.addAttribute("htmlBut", htmlBut);
		model.addAttribute("list", list);
		return "/security/resource/listSysResource";
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @param resources
	 *            获取查询条件
	 * @param page
	 *            获取当前页数,page是jqGrid自带参数
	 * @param rows
	 *            获取每页显示行数,rows是jqGrid自带参数
	 * @param sidx
	 *            获取排序列名,sidx是jqGrid自带参数
	 * @param sord
	 *            获取排序方式,sord是jqGrid自带参数
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(HttpServletRequest request,
			HttpServletResponse response,
			@Param(value = "resources") SysResource resources,
			@Param(value = "page") String page,
			@Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx,
			@Param(value = "sord") String sord) {
		pageView = resourcesService.query(getPageView(page, rows, sidx, sord),
				resources);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageView.getRecords());
		map.put("records", pageView.getRowCount());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}

	/**
	 * 编辑页面初始化加载
	 * 
	 * @param model
	 * @param resourcesId
	 * @return
	 */
	@RequestMapping("editResource")
	public String editResource(Model model,
			@Param(value = "resourcesId") String resourcesId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", resourcesId);
		SysResource resources = resourcesService.getSysResourcesByMap(map);
		model.addAttribute("resources", resources);
		return "/security/resource/editSysResource";
	}

	/**
	 * 执行资源编辑操作
	 * 
	 * @param model
	 * @param Map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doEditResource")
	public Map<String, Object> doEditResource(Model model,
			@Param(value = "resources") SysResource resources) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = resourcesService.updateSysResources(resources);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "编辑菜单数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "编辑菜单数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 添加菜单
	 * 
	 * @param resources
	 * @return Map
	 */
	@RequestMapping("addResource")
	@ResponseBody
	public Map<String, Object> addResource(@Param(value = "t") SysResource t) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(t.getParentId()==null){
			t.setParentId(0);
		}
		String errmsg = "";
		String errcode = resourcesService.addSysResources(t);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "录入菜单数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "录入菜单数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 根据ID删除菜单
	 * 
	 * @param model
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delResource")
	public Map<String, Object> delResource(Model model,
			@Param(value = "ids") String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = resourcesService.deleteSysResource(ids);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "批量删除菜单数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "批量删除菜单数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 录入角色用户数据
	 * 
	 * @param roleId
	 * @param resourceIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addRoleRes")
	public Map<String, Object> addRoleRes(
			@Param(value = "roleId") String roleId,
			@Param(value = "resourceIds") String resourceIds) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = resourcesService.addRoleRes(roleId, resourceIds);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "录入权限数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "录入权限数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;

	}

	/**
	 * 验证reskey
	 * 
	 * @param resKey
	 */
	@ResponseBody
	@RequestMapping(value = "checkResKey")
	public Map<String, Object> checkResKey(HttpServletRequest request,
			HttpServletResponse response, @Param(value = "resKey") String resKey) {
		String errcode = CommUtils.SUCC;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resKey", resKey);
		SysResource sysResource = resourcesService.getSysResourcesByMap(map);
		if (sysResource != null) {
			errcode = CommUtils.ERROR;
		} else {
			errcode = CommUtils.SUCC;
		}
		String errmsg = "";
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "菜单key不存在！";
			break;
		case CommUtils.ERROR:
			errmsg = "菜单key存在,请重新输入！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;

	}

	/**
	 * 获取登录用户菜单
	 * 
	 * @param resources
	 * @return
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "getMenuHtml")
	@ResponseBody
	public String getMenuHtml(HttpServletRequest request,
			HttpServletResponse response) {
		String menuStr = MenuUtil.getSysResourcesHtml(request);
		return menuStr;
	}

	/**
	 * 加载jstree数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataJsTree")
	public List<JSONObject> loadDataJsTree(HttpServletRequest request,
			HttpServletResponse response) {
		List<JSONObject> treeList = new ArrayList<>();

		SysResource t = new SysResource();
		t.setParentId(0);
		List<SysResource> list = resourcesService.queryAll(t);
		for (SysResource sysResource : list) {
			treeList.add(getResourceJosn(sysResource));
		}
		return treeList;
	}

	/**
	 * 把菜单对象转换为jstree树对应的json对象
	 * 
	 * @param resources
	 * @return
	 */
	public JSONObject getResourceJosn(SysResource resources) {
		JSONObject stateJson = new JSONObject();
		JSONObject json = new JSONObject();
		json.put("id", resources.getId());
		json.put("text", resources.getName());
		if (resources.getChildren() != null
				&& resources.getChildren().size() > 0) {
			stateJson.put("opened", true);
			json.put("state", "open");
			JSONArray jsonArray = new JSONArray();
			for (SysResource res : resources.getChildren()) {
				jsonArray.add(getResourceJosn(res));
			}
			json.put("children", jsonArray);
		} else {
			stateJson.put("opened", false);
		}
		json.put("state", stateJson);
		return json;
	}

	/**
	 * 根据角色获取菜单
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	@ResponseBody
	@RequestMapping(value = "findResourcesByRole")
	public List<SysResource> findResourcesByRole(HttpServletRequest request,
			HttpServletResponse response, @Param(value = "roleId") String roleId) {
		List<SysResource> list = resourcesService.findRoleRes(roleId);
		return list;
	}
	/**
	 * 加载菜单树
	 * @param request
	 * @param response
	 * @param resourceid
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "getMenuJson")
	public List<Object> getMenuJson(HttpServletRequest request,
			HttpServletResponse response,@Param("parentid")String parentid) throws Exception {
		String resourceid=request.getParameter("id");
		List<Object> list = resourcesService.getMenuJson(resourceid,parentid);
		return list;
	}
}